#include "gpio.h"
#include "spi.h"
#include "tim.h"
#include "ad7606.h"

static void AD7606Reset(void)
{
    /*! ___|-----|________  >= 50ns */
    AD7606Rst_Low();
    AD7606Rst_High();
    for(int i = 20; i > 0; i--){
        __NOP();//1000/168 ns = 5.85ns
    }
    AD7606Rst_Low();
}

float AD7606ConvValue(uint16_t bin)
{
    int _val;
    float adcValue;
    _val = bin&0x8000 ? (-((~bin+1)&0x7fff)) : bin;
    adcValue = 5.0*_val/32768.0;
    return adcValue;
}

void AD7606Init(void)
{
    AD7606Cs_High();
    AD7606Reset();
}

void AD7606Start(void)
{
    HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);
    HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
}

void AD7606Stop(void)
{
    HAL_NVIC_DisableIRQ(EXTI9_5_IRQn);
    HAL_TIM_PWM_Stop(&htim3,TIM_CHANNEL_1);
    AD7606Cs_High();
    
}

void AD7606BusyIrqCallback(uint16_t *ad7606Val,uint8_t ad7606Chl)
{
    AD7606Cs_Low();
    HAL_SPI_Receive(&hspi2,(uint8_t *)ad7606Val,ad7606Chl,10000);
    AD7606Cs_High();
}
